package com.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stu.mapper.*;
import com.stu.pojo.*;
import com.stu.pojo.vo.StudentVo;
import com.stu.service.IStuAService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-12-20
 */
@Service
public class StuAServiceImpl extends ServiceImpl<StuAMapper, StuA> implements IStuAService {
    @Autowired
    StuAMapper stuAMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    StudentDescMapper studentDescMapper;
    @Autowired
    ClazzMapper clazzMapper;
    @Autowired
    ChannelMapper channelMapper;

    //新增待办学生
    public boolean insertStuA(StuA stuA){
        stuA.setStatus("未通过");
        stuA.setSid(getStuid());
        int insert = stuAMapper.insert(stuA);
        return insert > 0;
    }

    //审核通过
    @Override
    public boolean pass(StudentVo studentVo) {
        //通过clazzName查询id
        String clazzName = studentVo.getClazzName();
        LambdaQueryWrapper<Clazz> qw = new LambdaQueryWrapper<>();
        qw.eq(Clazz::getName,clazzName);
        Long clazzId = clazzMapper.selectOne(qw).getId();
        //通过channelName查询id
        String channelName = studentVo.getChannelName();
        LambdaQueryWrapper<Channel> cqw = new LambdaQueryWrapper<>();
        cqw.eq(Channel::getName,channelName);
        Long channelId = channelMapper.selectOne(cqw).getId();
        //改变状态
        studentVo.setStatus("已通过");
        studentVo.setClazzId(clazzId);
        stuAMapper.updateById(studentVo);
        //新增学生
        Student student = new Student();
        BeanUtils.copyProperties(studentVo,student);
        student.setStatus("在学");
        student.setClazzId(clazzId);
        student.setClazzName(clazzName);
        studentMapper.insert(student);
        //新增学生详情
        StudentDesc studentDesc = new StudentDesc();
        BeanUtils.copyProperties(studentVo,studentDesc);
        studentDesc.setChannelId(channelId);
        studentDesc.setChannelName(channelName);
        studentDesc.setPayStatus("未完成");
        studentDesc.setPayCount(0.0);
        studentDescMapper.insert(studentDesc);
        return true;
    }


    public Long getStuid(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String left = sdf.format(date);
        System.out.println(left);
        LambdaQueryWrapper<StuA> qw = new LambdaQueryWrapper<>();
        qw.like(StuA::getSid,left);
        Integer count = stuAMapper.selectCount(qw);
        long result;
        if (count > 0){
            String sid = stuAMapper.getSid();
            long i = Long.parseLong(sid);
            i = i + 1;
            result = i;
        } else {
            String re = left + "001";
            result = Long.parseLong(re);
        }
        return result;
    }
}
